<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<title>-</title>
	<link rel="stylesheet" href='<c:url value="/resources/css/main.css"/>' type="text/css" />
	<link rel="stylesheet" type="text/css" href='<c:url value="/resources/easyui/themes/default/easyui.css"/>'/>
	<link rel="stylesheet" type="text/css" href='<c:url value="/resources/easyui/themes/icon.css"/>'/>
	<script type="text/javascript" src='<c:url value="/resources/easyui/jquery-1.4.4.min.js"/>'></script>
	<script type="text/javascript" src='<c:url value="/resources/easyui/jquery.easyui.min.js"/>'></script>
	<script type="text/javascript">
	var drivers = [];
	var urlPatterns = [];
	var validationQueries = [];
	var hibernateDialects = [];
	<c:forEach var="database" items="${databases}">
		drivers['${database.type}'] = '${database.driver}';
		urlPatterns['${database.type}'] = '${database.urlPattern}';
		validationQueries['${database.type}'] = '${database.validationQuery}';
		hibernateDialects['${database.type}'] = '${database.hibernateDialect}';
	</c:forEach> 
	
	$(function(){
		$('#databaseType').val('${databaseType}');
		$('#databaseType').change(function(opt) {
			var dbType = $('#databaseType').val();
			$('#databaseDriver').val(drivers[dbType]);
			$('#databaseUrl').val(urlPatterns[dbType]);
			$('#databaseUsername').val('');
			$('#databasePassword').val('');
			$('#databaseValidationQuery').val(validationQueries[dbType]);
			$('#hibernateDialect').val(hibernateDialects[dbType]);
		});
		
		$('#cancel').click(function() {
			$('#f')[0].reset();
		});
		
		$('#save').click(function() {
			$.ajax({
				url: "<c:url value='/admin/system/update/database'/>",
				type: "POST",
				dataType: "json",
				data: ({
					databaseType: $('#databaseType').val(),
					databaseDriver: $('#databaseDriver').val(),
					databaseUrl: $('#databaseUrl').val(),
					databaseUsername: $('#databaseUsername').val(),
					databasePassword: $('#databasePassword').val(),
					databaseValidationQuery: $('#databaseValidationQuery').val(),
					hibernateDialect: $('#hibernateDialect').val()
				}),
				success: function(msg) {
					if (msg.result === 'ok') {
						alert('数据库设置已经保存');
					}
					else {
						alert('保存失败：\n' + msg.message);
					}
				}
			});
		});
	});
	</script>
</head>
<body id="jive-body">
<div id="main">
	<table cellpadding="0" cellspacing="0" border="0" width="100%">
	<tbody>
		<tr valign="top">
			<td width="99%" id="jive-content">
 				<h1>数据库设置</h1>
				<div id="jive-main-content">
					<p>使用下面的表单设置数据库信息：</p>
<p/>

<div class="warning">
<table cellpadding="0" cellspacing="0" border="0" >
<tbody>
    <tr>
        <td class="jive-icon-label">
        <b>需要应用服务器重新启动</b><br/><br/>
        </td>
    </tr>
    <td valign="top" align="left" colspan="2">
        <span>
        	修改数据库设置会更新/WEB-INF/classes/jdbc.properties文件，需要应用服务器重新启动后生效。
        </span>
    </td>
</tbody>
</table>
</div>
<br>

<form action="#" id="f" method="post">
	<div class="jive-contentBoxHeader">
		数据库设置
	</div>
	<div class="jive-contentBox">
		<table width="80%" cellpadding="3" cellspacing="0" border="0">
		<tr>
			<td nowrap>
				数据库
			</td>
			<td nowrap>
				<select id="databaseType" style="width:160px">
					<c:forEach var="database" items="${databases}">
						<option value="${database.type}">${database.type}</option>
					</c:forEach>
				</select>
			</td>
		</tr>
		<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td nowrap>
				JDBC 驱动程序
			</td>
			<td nowrap>
				<input type="text" id="databaseDriver" value="<c:out value='${databaseDriver}'/>" size="80" maxlength="255">
			</td>
		</tr>
		<tr>
			<td nowrap>
				DB 连接 URL
			</td>
			<td nowrap>
				<input type="text" id="databaseUrl" value="<c:out value='${databaseUrl}'/>" size="80" maxlength="255">
			</td>
		</tr>
		<tr>
			<td nowrap>
				DB 用户
			</td>
			<td nowrap>
				<input type="text" id="databaseUsername" value="<c:out value='${databaseUsername}'/>" size="40" maxlength="255">
			</td>
		</tr>
		<tr>
			<td nowrap>
				DB 密码
			</td>
			<td nowrap>
				<input type="password" id="databasePassword" value="<c:out value='${databasePassword}'/>" size="40" maxlength="255">
			</td>
		</tr>
		<tr>
			<td nowrap>
				Database Validation SQL
			</td>
			<td nowrap>
				<input type="text" id="databaseValidationQuery" value="<c:out value='${databaseValidationQuery}'/>" size="40" maxlength="255">
			</td>
		</tr>
		<tr>
			<td nowrap>
				Hibernate Dialect
			</td>
			<td nowrap>
				<input type="text" id="hibernateDialect" value="<c:out value='${hibernateDialect}'/>" size="40" maxlength="255">
			</td>
		</tr>
		</table>
	</div>
<input type="button" id="save" value="保存设置">
<input type="button" id="cancel" value="取消">
</form>

				</div>
			</td>
		</tr>
	</tbody>
	</table>
</div>
</body>
</html>
